package com.focusai.efairy.model.request;

import com.focusai.efairy.model.manager.CacheManager;
import com.focusai.efairy.model.response.DevMsgListResponse;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.ParseException;
import com.focusai.efairy.network.request.base.Request;
import com.focusai.efairy.network.utils.UrlUtils;
import com.focusai.efairy.utils.GsonHelper;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;

import java.util.Map;

/**
 * Created by luo_guangwen on 2017/8/12.
 */

public class GetDevMsgListRequest extends Request<DevMsgListResponse> {
    private long deviceId;   //设备编号
    private long last_id;   //最后一条记录的编号
    private int size;
    private int alarm_id = -1; //默认不传则返回所有，报警类型：0-离线 1-火警 2-预警 3-故障 4-启动 5-屏蔽 6-正常
    public GetDevMsgListRequest(long deviceId, long lastId, int size,int alarm_id, Response.Listener<DevMsgListResponse> listener) {
        super(Method.GET, UrlUtils.createUrl("/appapi/device_msglist"), listener);
        this.deviceId = deviceId;
        this.last_id = lastId;
        this.size = size;
        this.alarm_id = alarm_id;
    }

    @Override
    public Map<String, String> getParams() throws JSONException {
        addParam("efairyuser_id", CacheManager.getUserId());
        addParam("access_token", CacheManager.getAccessToken());
        addParam("efairydevice_id", String.valueOf(deviceId));
        addParam("last_id", String.valueOf(last_id));
        addParam("size", String.valueOf(size));
        if (this.alarm_id != -1){
            addParam("alarm_id", String.valueOf(alarm_id));
        }
        return super.getParams();
    }

    @Override
    protected DevMsgListResponse parse(String result) throws ParseException {
        return GsonHelper.getInstance().fromJson(result, new TypeToken<DevMsgListResponse>() {
        }.getType());
    }

}
